package com.caishi.lkx.user.userlog.model;

import cn.hutool.core.map.MapUtil;
import com.caishi.lkx.user.ienum.type.UserLogSourceType;
import com.fasterxml.jackson.annotation.JsonProperty;

import com.caishi.lkx.user.userlog.vo.ClientCountVo;
import org.springframework.data.annotation.Transient;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Stream;


public class Count implements Serializable {

    private CountItem PcZhiYe = new CountItem();

    private CountItem PcGjtk = new CountItem();

    private CountItem AppZhiYe = new CountItem();

    private CountItem AppGjtk = new CountItem();

    //private CountItem WpZhiYeApp = new CountItem();

    private CountItem WpZSZhiYe = new CountItem();

    private CountItem WpZSGjtk = new CountItem();

    @Transient
    public CountItem getAll() {
        var all = new CountItem();
        all.allAll(PcZhiYe, false);
        all.allAll(PcGjtk, false);
        all.allAll(AppZhiYe, false);
        all.allAll(AppGjtk, false);
       // all.allAll(WpZhiYeApp, false);
        all.allAll(WpZSZhiYe, false);
        all.allAll(WpZSGjtk, false);
        return all;
    }

    public Count setCount(UserLogSourceType type, CountItem data) {
        getTypeItem(type).allAll(data, true);
        return this;
    }

    public Count addAllCount(UserLogSourceType type, double count, boolean addToday) {
        var item = getTypeItem(type);
        item.addAllCount(count);
        if (addToday) {
            item.addTodayCount(count);
        }
        var exams = item.getExam();
        // 直接新增总数时 将新增的总数平均分配到各个考试
        if (MapUtil.isNotEmpty(exams)) {
            var c = count / exams.size();
            exams.forEach((k, v) -> {
                v.addAllCount(c);
                if (addToday) {
                    v.addTodayCount(c);
                }
            });
        }
        return this;
    }


    public Count addAll(Count data, boolean setExam) {
        PcZhiYe.allAll(data.PcZhiYe, setExam, true);
        PcGjtk.allAll(data.PcGjtk, setExam, true);
        AppZhiYe.allAll(data.AppZhiYe, setExam, true);
        AppGjtk.allAll(data.AppGjtk, setExam, true);
       // WpZhiYeApp.allAll(data.WpZhiYeApp, setExam, true);
        WpZSZhiYe.allAll(data.WpZSZhiYe, setExam, true);
        WpZSGjtk.allAll(data.WpZSGjtk, setExam, true);
        return this;
    }

    public Count addAll1(Count data, boolean setExam) {
        PcZhiYe.allAll(data.PcZhiYe, setExam, false);
        PcGjtk.allAll(data.PcGjtk, setExam, false);
        AppZhiYe.allAll(data.AppZhiYe, setExam, false);
        AppGjtk.allAll(data.AppGjtk, setExam, false);
        // WpZhiYeApp.allAll(data.WpZhiYeApp, setExam, true);
        WpZSZhiYe.allAll(data.WpZSZhiYe, setExam, false);
        WpZSGjtk.allAll(data.WpZSGjtk, setExam, false);
        return this;
    }
    public ClientCountVo toClient() {
        var client = new ClientCountVo();
        client.setAll(this.getAll().toClient());
        client.setPcZhiYe(this.PcZhiYe.toClient());
        client.setPcGjtk(this.PcGjtk.toClient());
        client.setAppZhiYe(this.AppZhiYe.toClient());
        client.setAppGjtk(this.AppGjtk.toClient());
       // client.setWpZhiYeApp(this.WpZhiYeApp.toClient());
        client.setWpZSZhiYe(this.WpZSZhiYe.toClient());
        client.setWpZSGjtk(this.WpZSGjtk.toClient());
        return client;
    }

    public Count today2All() {
        PcZhiYe.today2All();
        PcGjtk.today2All();
        AppZhiYe.today2All();
        AppGjtk.today2All();
       // WpZhiYeApp.today2All();
        WpZSZhiYe.today2All();
        WpZSGjtk.today2All();
        return this;
    }

    public CountItem getTypeItem(UserLogSourceType type) {
        if (type == UserLogSourceType.PcZhiYe) return PcZhiYe;
        if (type == UserLogSourceType.PcGjtk) return PcGjtk;
        if (type == UserLogSourceType.AppZhiYe) return AppZhiYe;
        if (type == UserLogSourceType.AppGjtk) return AppGjtk;
        //if (type == UserLogSourceType.WpZhiYeApp) return WpZhiYeApp;
        if (type == UserLogSourceType.WpZSZhiYe) return WpZSZhiYe;
        if (type == UserLogSourceType.WpZSGjtk) return WpZSGjtk;
        return new CountItem();
    }

    public void copyLast(Count last) {
        this.PcZhiYe.addAllCount(last.PcZhiYe.getAllCount())
                .addRealAllCount(last.PcZhiYe.getRealAllCount());
        this.PcGjtk.addAllCount(last.PcGjtk.getAllCount())
                .addRealAllCount(last.PcGjtk.getRealAllCount());
        this.AppZhiYe.addAllCount(last.AppZhiYe.getAllCount())
                .addRealAllCount(last.AppZhiYe.getRealAllCount());
        this.AppGjtk.addAllCount(last.AppGjtk.getAllCount())
                .addRealAllCount(last.AppGjtk.getRealAllCount());
//        this.WpZhiYeApp.addAllCount(last.WpZhiYeApp.getAllCount())
//                .addRealAllCount(last.WpZhiYeApp.getRealAllCount());
        this.WpZSZhiYe.addAllCount(last.WpZSZhiYe.getAllCount())
                .addRealAllCount(last.WpZSZhiYe.getRealAllCount());
        this.WpZSGjtk.addAllCount(last.WpZSGjtk.getAllCount())
                .addRealAllCount(last.WpZSGjtk.getRealAllCount());
    }

    public Count sourceData(List<UserLogSourceType> sourceTypes, boolean setExam) {
        var result = new Count();
        if (sourceTypes.contains(UserLogSourceType.PcZhiYe)) result.PcZhiYe.allAll(this.PcZhiYe, setExam);
        if (sourceTypes.contains(UserLogSourceType.PcGjtk)) result.PcGjtk.allAll(this.PcGjtk, setExam);
        if (sourceTypes.contains(UserLogSourceType.AppZhiYe)) result.AppZhiYe.allAll(this.AppZhiYe, setExam);
        if (sourceTypes.contains(UserLogSourceType.AppGjtk)) result.AppGjtk.allAll(this.AppGjtk, setExam);
        //if (sourceTypes.contains(UserLogSourceType.WpZhiYeApp)) result.WpZhiYeApp.allAll(this.WpZhiYeApp, setExam);
        if (sourceTypes.contains(UserLogSourceType.WpZSZhiYe)) result.WpZSZhiYe.allAll(this.WpZSZhiYe, setExam);
        if (sourceTypes.contains(UserLogSourceType.WpZSGjtk)) result.WpZSGjtk.allAll(this.WpZSGjtk, setExam);
        return result;
    }

    public List<UserLogSourceType> validSource() {
        return Stream.of(UserLogSourceType.values())
                .filter(type -> getTypeItem(type).getAllCount() > 0)
                .toList();
    }

    @JsonProperty("PcZhiYe")
    public CountItem getPcZhiYe() {
        return PcZhiYe;
    }

    @JsonProperty("PcGjtk")
    public CountItem getPcGjtk() {
        return PcGjtk;
    }

    @JsonProperty("AppZhiYe")
    public CountItem getAppZhiYe() {
        return AppZhiYe;
    }

    @JsonProperty("AppGjtk")
    public CountItem getAppGjtk() {
        return AppGjtk;
    }

//    @JsonProperty("WpZhiYeApp")
//    public CountItem getWpZhiYeApp() {
//        return WpZhiYeApp;
//    }

    @JsonProperty("WpZSZhiYe")
    public CountItem getWpZSZhiYe() {
        return WpZSZhiYe;
    }

    @JsonProperty("WpZSGjtk")
    public CountItem getWpZSGjtk() {
        return WpZSGjtk;
    }

    public void setPcZhiYe(CountItem pcZhiYe) {
        PcZhiYe = pcZhiYe;
    }

    public void setPcGjtk(CountItem pcGjtk) {
        PcGjtk = pcGjtk;
    }

    public void setAppZhiYe(CountItem appZhiYe) {
        AppZhiYe = appZhiYe;
    }

    public void setAppGjtk(CountItem appGjtk) {
        AppGjtk = appGjtk;
    }

//    public void setWpZhiYeApp(CountItem wpZhiYeApp) {
//        WpZhiYeApp = wpZhiYeApp;
//    }

    public void setWpZSZhiYe(CountItem wpZSZhiYe) {
        WpZSZhiYe = wpZSZhiYe;
    }

    public void setWpZSGjtk(CountItem wpZSGjtk) {
        WpZSGjtk = wpZSGjtk;
    }
}
